import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { DeliverymanInfoDTO } from '@/types/driver'

// 定义 Store
export const useDriverStore = defineStore(
  'driver',
  () => {
    // 用户信息
    const deliverymanInfo = ref<DeliverymanInfoDTO>()

    const getDeliverymanInfo = () => {
      return deliverymanInfo.value
    }
    const setDeliverymanInfo = (val: DeliverymanInfoDTO) => {
      deliverymanInfo.value = val
    }
    // 清理会员信息，退出时使用
    const clearDeliverymanInfo = () => {
      deliverymanInfo.value = {} as DeliverymanInfoDTO
    }

    // 记得 return
    return {
      getDeliverymanInfo,
      setDeliverymanInfo,
      clearDeliverymanInfo,
    }
  },
  {
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
